package com.android.webserver.fwapi;


import java.util.HashMap;

import android.net.UrlQuerySanitizer;
import android.net.UrlQuerySanitizer.ValueSanitizer;
import android.util.Log;

public class Request
{
	private HashMap<String, String>	headers	= new HashMap<String, String>();
	private HashMap<String, String>	cookies	= new HashMap<String, String>();
	private float										protocolVersion;
	private String									requestURI;
	private int											requestType;
	private String									requestBody;
	UrlQuerySanitizer								uqs;	

	public static int				POST_REQUEST = 0;
	public static int				GET_REQUEST = 1;
	
	public Request(String requestURI, String requestType, String rawHeaders,
			String rawBody)
	{
		this.requestURI = requestURI;
		if (requestType.equals("POST"))
			this.requestType = POST_REQUEST;
		else
			this.requestType = GET_REQUEST;
		this.requestBody = rawBody;
		readHeaders(rawHeaders);
	}

	public String getParameter(String parameter)
	{
		return uqs.getValue(parameter);
	}

	public String getParameter(String parameter, ValueSanitizer valueSanitizer)
	{
		uqs = new UrlQuerySanitizer();
		uqs.setAllowUnregisteredParamaters(true);
		
		uqs.registerParameter(parameter, valueSanitizer);
		if (requestType == GET_REQUEST)
			uqs.parseUrl(requestURI);
		else
			if (requestType == POST_REQUEST)
				uqs.parseQuery(requestBody);
		return uqs.getValue(parameter);
	}

	
	private void readHeaders(String rawHeaders)
	{
		while (true)
		{
			int pos = rawHeaders.indexOf('\n');
			if (pos == -1)
				break;

			String header = rawHeaders.substring(0, pos);
			processHeader(header);
			rawHeaders = rawHeaders.substring(pos + 1);
		}

		uqs = new UrlQuerySanitizer();
		uqs.setAllowUnregisteredParamaters(true);
		
		
		if (requestType == GET_REQUEST)
			uqs.parseUrl(requestURI);
		else
			if (requestType == POST_REQUEST)
				uqs.parseQuery(requestBody);
		
	}

	private void processHeader(String header)
	{
		int split = header.indexOf(':', 3);
		if (split == -1)
			return; // ignore malformed headers

		String key = header.substring(0, split);
		String val = header.substring(split + 1).trim();

		if (key.equals("Cookie"))
			processCookieHeader(val);
		headers.put(key, val);
	}

	private void processCookieHeader(String cookie)
	{
		String[] cookieFields = cookie.split(":");
		for (int i = 0; i < cookieFields.length; i++)
		{
			int split = cookieFields[i].indexOf('=');
			if (split != -1)
			{
				String key = cookieFields[i].substring(0, split);
				String value = cookieFields[i].substring(split + 1);
				cookies.put(key, value);
			}
		}
	}

	public String getLogName()
	{
		return null;
	}

	public float getProtocolVersion()
	{
		return protocolVersion;
	}

	public String getURI()
	{
		return requestURI;
	}

	public int getType()
	{
		return requestType;
	}

	public String getHeader(String key)
	{
		return (String) headers.get(key);
	}

	protected void dump()
	{
		Log.d("WebServer", "==== URI : " + getURI());
		Log.d("WebServer", "==== TYPE : " + getType());

		Log.d("WebServer", "==== HEADERS ====");
		for (String key : headers.keySet())
			Log.d("WebServer", key + " = " + headers.get(key));
		if (requestBody != null)
			Log.d("WebServer", "==== rawBody : " + requestBody);
	}

}
